/**
 * This class represents a product group
 */

/**
 * @author Konrad Paumann
 * @author Maximilian Csuk
 * @author Bernhard Denner
 * 
 * $Revision$
 * $Date$
 *
 */
import java.util.*;

public class ProductGroup implements Displayable {
	/**
	 * holds name of the product group
	 */
	private String groupName;
	private int tax;

	/**
	 * contains stored product groups
	 */
	private TreeMap<String, Product> productList;

	/**
	 * constructor
	 */
	ProductGroup(String groupName, int tax) {
		productList = new TreeMap<String, Product>();
		this.groupName = groupName;
		this.tax = tax;
	}

	/**
	 * returns the name of the product group
	 * 
	 * @return groupName
	 */
	public String getGroupName() {
		return groupName;
	}

	/**
	 * sets the name of the product group (groupName)
	 * 
	 * @param groupName
	 */
	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	/**
	 * returns a product
	 * 
	 * @param sDescription
	 * @return product
	 * @throws ProductException
	 */
	public Product getProduct(String sDescription) throws ProductException {
		if (!containsProduct(sDescription))
			throw new ProductException("Produkt \"" + sDescription + "\" nicht im Sortiment");

		return productList.get(sDescription);
	}

	/**
	 * add a product into the assortment
	 * 
	 * @param sDescription
	 * @param description
	 * @param price
	 * @param amount
	 * @throws ProductException
	 */
	public void addProduct(Product p) throws ProductException {
		if (productList.containsKey(p.getShortDescription())) {
			throw new ProductException("Produkt bereits enthalten");
		} else {
			productList.put(p.getShortDescription(), p);
		}
	}
	
	/**
	 * removes the product
	 * @param sDescription
	 */
	public void removeProduct(String sDescription) throws ProductException {
		if (productList.remove(sDescription) == null)
			throw new ProductException("Produkt nicht gefunden");		
	}

//	/**
//	 * lists all products
//	 */
//	public void listProducts() {
//		Iterator<String> it = productList.keySet().iterator();
//		while (it.hasNext()) {
//			System.out.println(productList.get(it.next()));
//		}
//	}

	/**
	 * checks if product is in product group
	 * 
	 * @param sDescription
	 * @return true product in group false product not in group
	 */
	public boolean containsProduct(String sDescription) {
		if (productList.containsKey(sDescription)) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * returns if the productList is empty
	 * @return 	true if empty
	 * 			false if not empty
	 */
	public boolean isEmpty() {
		//TODO
		return productList.isEmpty();
	}

	/**
	 * returns a product iterator
	 */
	public Iterator<Product> getProductIterator() {
		return productList.values().iterator();
	}
	
	/**
	 * returns the tax
	 * @return tax
	 */
	public int getTax() {
		return tax;
	}

	/**
	 * sets the tax
	 * @param tax
	 */
	public void setTax(int tax) {
		this.tax = tax;
	}
	
	/**
	 * gets the string to be displayed
	 * 
	 * @return String with all products
	 */
	public String getDisplayString() {
		String display = new String("");
		Iterator<String> it =  productList.keySet().iterator();
		display += "Group-name: " + groupName + "\n";
		display += "tax: " + tax + "\n";
		while (it.hasNext()) {
			display +=  productList.get(it.next()).getDisplayString() + "\n";
		}
		return display;
	}
	
	public String save() {
		return getGroupName() + ":" + getTax();
	}
	
	
	/**
	 * adds a Product to the assortment, which will be created by the 
	 * restore string
	 * @param ProductLine
	 * @throws ProductException
	 */
	public void restore(String ProductLine) throws ProductException {
		addProduct(new Product(ProductLine));
	}
}
